﻿using System;
using CARMSBusiness;
using CARMSData.EntityClasses;


namespace CARMS.Admin
{
 
    public partial class DictionaryItemCreate :PageBase
    {
        private int CurrentTypeID
        {
            get
            {
                if (ViewState["CurrentTypeID"] == null)
                {
                    ViewState["CurrentTypeID"] = Request.QueryString["TypeID"];
                }
                return int.Parse(ViewState["CurrentTypeID"].ToString());
            }
            set { ViewState["CurrentTypeID"] = value; }
        }

        private int CurrentItemID
        {
            get
            {
                if (ViewState["CurrentItemID"] == null)
                {
                    ViewState["CurrentItemID"] = GetGuidByParameter("ItemID");
                }
                return int.Parse(ViewState["CurrentItemID"].ToString());
            }
            set { ViewState["CurrentItemID"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadDefaultItem();
            }
        }

        private void LoadDefaultItem()
        {
            var item = AdminBusiness.GetDictionary(CurrentItemID);
            if(item!=null)
            {
                txtItemName.Text = item.Name.Trim();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            CloseWindow(false);
        }
        protected void btnSaveType_Click(object sender, EventArgs e)
        {
            if (txtItemName.Text.Trim().Length == 0)
            {
                base.Alert("Please input item name！");
            }
            else
            {
                var entity = new DictionaryEntity(CurrentItemID);
                if(entity.IsNew)
                {
                    entity.CategoryId = CurrentTypeID;
                }
                entity.Name = txtItemName.Text.Trim();

                if (AdminBusiness.SaveDictionary(entity))
                {
                    CloseWindow(true);
                }
                else
                {
                    CloseWindow(false);
                }
            }
        }
    }
}
